package com.example.server.controller;

import com.example.server.service.FileService;
import com.example.server.util.DealFiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

/**
 * (File)表控制层
 *
 * @author makejava
 * @since 2021-11-16 17:57:08
 */
@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
public class FileController {


    @Resource
    private FileService fileService;

    @Resource
    private DealFiles dealFiles;

    @RequestMapping("/upload")
    public String upload(@RequestParam("file")MultipartFile multipartFile){
        if(multipartFile.getOriginalFilename().equals("")||multipartFile.getOriginalFilename()==null){
            return "no file";
        }
        return fileService.insert(dealFiles.dealFile(multipartFile));
    }

    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response, String uuid){
//        File file=new File(filepath+"\\"+id);
        com.example.server.pojo.File file= fileService.queryById(uuid);
        String fileallpath=file.getFilePath();
        String contentType="application/octet-stream";
        response.setContentType("text/html;charset=utf-8");
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        java.io.File f = new File(fileallpath);
        if (!f.exists()){
            return "401";
        }
        try {
            request.setCharacterEncoding("UTF-8");
            long length=f.length();
            InputStream in = new FileInputStream(f);
            response.setContentType(contentType);
            response.setHeader("Content-disposition",
                    "attachment; filename=" + new String((file.getFileName()+"."+ file.getFileType()).getBytes("utf-8"), "ISO8859-1"));
            response.setHeader("Content-length", String.valueOf(length));
            bis=new BufferedInputStream(in);
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff=new byte[1024];
            int bytesRead;
            while ((bytesRead=bis.read(buff,0,buff.length))!=-1){
                bos.write(buff,0,bytesRead);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "ok";
    }

    @RequestMapping("/getjson")
    public com.example.server.pojo.File getJson(String uuid){
        return fileService.queryById(uuid);
    }


}

